<?php

namespace App\Models;

use Core\Result\Result;
use Core\Sign\Sign;
use Core\Utils\Ip\Ipinit;

class UsersModel
{
    /**
     * 通过uuid获取用户id
     * entry conn,uuid
     * return id
     * */
    public static function getUserIdOnUuid($conn,$uuid){
        $sql = "select id from `users` where `uuid` = '$uuid'";
        return $conn->readConn($sql)["id"];
    }
    /**
     * 查询滑落id
     * entry conn
     * return id
     * */
    public static function getUserSid($conn){
        $sql = "select id from `users` where `sone` < 3 order by `id` asc limit 1";
        return $conn->readConn($sql)["id"];
    }
    /**
     * 获取用户信息
     * entry conn,token
     * return array
     * */
    public static function getUserInside($conn,$token){
        $queryMemberSql="SELECT `id`,`uuid`,`token`,`username`, `truename`,`pay_password`,`phone`,`head`, `pid`, `sid`, `level`,`level_time`, `group`, `money`, `tinymoney`, `venturemoney`, `integral`, `id_card`, `login_ip`, `create_time`, `last_time`, `status`, `ach`, `one_ach`, `sach`, `share_rewards`, `push_reward`, `peer_reward`, `agent_reward`, `partner_reward`,`total_reward`,`service` FROM `users` WHERE  `token` = '$token'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户信息(地址)
     * entry conn,token
     * return array
     * */
    public static function getUserPlaceInside($conn,$uid){
        $queryMemberSql="SELECT `province`,`city`,`area` FROM `users_place` WHERE  `uid` = '$uid'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户代理申请
     * entry conn,token
     * return array
     * */
    public static function getUserPasswordInside($conn,$token){
        $queryMemberSql="SELECT `id`,`uuid`,`token`,`password`, `pay_password`,`username`,`phone` FROM `users` WHERE  `token` = '$token'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户信息
     * entry conn,id
     * return array
     * */
    public static function getUserInsideId($conn,$id){
        $queryMemberSql="SELECT `id`,`uuid`,`token`, `username`, `truename`, `phone`,`head`, `pid`, `sid`, `level`,`level_time`, `money`, `integral`,`tinymoney`,`venturemoney`, `id_card`, `login_ip`, `create_time`, `last_time`, `status`, `ach`, `one_ach`, `sach`, `share_rewards`, `push_reward`, `peer_reward`, `agent_reward`, `partner_reward`,`service` FROM `users` WHERE  `id` = '$id'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取代理信息
     * entry conn,id
     * return array
     * */
    public static function getAgentInsideId($conn,$id){
        $queryMemberSql="SELECT `id`,`uuid`,`token`, `username`, `truename`, `phone`,`head`, `pid`, `agent`, `level`,`level_time`, `group`, `money`, `integral`, `login_ip`, `create_time`, `last_time`, `status`, `ach`, `one_ach`, `sach`, `share_rewards`, `push_reward`, `peer_reward`, `agent_reward`, `partner_reward`,`service`,`service_time` FROM `users` WHERE  `service` = '$id'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取总代理信息（count）
     * entry conn,id
     * return array
     * */
    public static function getGeneralAgencyCountInside($conn){
        $queryMemberSql="SELECT count(`id`) as total FROM `users` WHERE  `agent` = 4";
        return $conn->readConn($queryMemberSql)["total"];
    }
    /**
     * 获取总代理信息
     * entry conn,id
     * return array
     * */
    public static function getGeneralAgencyInside($conn){
        $queryMemberSql="SELECT `id`,`uuid`,`token`, `username`, `truename`, `phone`,`head`, `pid`, `agent`, `level`,`level_time`, `group`, `money`, `integral`,  `login_ip`, `create_time`, `last_time`, `status`, `ach`, `one_ach`, `sach`, `share_rewards`, `push_reward`, `peer_reward`, `agent_reward`, `partner_reward`,`service`,`service_time` FROM `users` WHERE  `agent` = 4";
        return $conn->readConn($queryMemberSql,true);
    }
    /**
     * 获取用户姓名
     * entry conn,id
     * return array
     * */
    public static function getUserNameInsideId($conn,$id){
        $queryMemberSql="SELECT `id`,`uuid`, `username`, `truename`, `phone`,`head` FROM `users` WHERE  `id` = '$id'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户信息
     * entry conn,username
     * return array
     * */
    public static function getUserInsideUsername($conn,$username){
        $queryMemberSql="SELECT `id`,`uuid`,`token`,`password`,`status`,`username`,`truename`,`money`,`venturemoney` FROM `users` WHERE  `username` = '$username'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户信息
     * entry conn,uuid
     * return array
     * */
    public static function getUserInsideUuid($conn,$uuid){
        $queryMemberSql="SELECT `id`,`uuid`,`token`,`pid`,`password`,`status`,`money`,`venturemoney`,`username`,`truename`,`tinymoney` FROM `users` WHERE  `uuid` = '$uuid'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 插入用户信息
     * entry conn,token
     * return int
     * */
    public static function insertUserInside($conn,$UserExample){
        $insertSql = "insert into `users`(`uuid`,`token`,`username`,`password`,`phone`,`pid`,`sid`,`create_time`)value('$UserExample->uuid','$UserExample->token','$UserExample->username','$UserExample->password','$UserExample->phone',$UserExample->pid,$UserExample->sid,$UserExample->create_time)";
        return $conn->executeConn($insertSql);
    }
    /**
     * 查询用户状态
     * entry conn,uid
     * return int
     * */
    public static function queryUserStateInside($conn,$uid){
        $queryMemberSql="SELECT `uid`,`switch`,`switch_f`,`switch_z`,`switch_h`,`switch_j`,`switch_g`,`switch_gq` FROM `u_k` WHERE  `uid` = '$uid'";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 查询所有上级
     * entry conn,uid
     * return int
     * */
    public static function getUserSuperiorAllIds($conn,$id){
        $queryMemberSql="SELECT GROUP_CONCAT(`id`) as `ids` FROM (SELECT @r AS _id,(SELECT @r := pid FROM users WHERE id = _id) AS parent_id,@l := @l + 1 AS lvl FROM (SELECT @r := $id, @l := 0) vars, users h WHERE @r <> 0) T1 JOIN users T2 ON T1._id = T2.id where 1=1 ORDER BY lvl ASC";
        return $conn->readConn($queryMemberSql)["ids"];
    }
    /**
     * 登录成功
     * entry conn,token
     * return json
     * */
    public static function loginSuccess($conn,$UserExample)
    {
        $UserExample->login_ip =new Ipinit();
        $UserExample->last_time = "NOW()";
        self::upUserInside($conn, $UserExample);
        $sign = new Sign();
        Result::success($sign->encryptionToken($UserExample->token));
    }
    /**
     * 通过openid获取用户信息
     * entry conn,openid
     * return array
     * */
    public static function adoptOpenidGetUserData($conn,$openid){
        $memberSql = "SELECT id,`token`,`phone`,status FROM `users` WHERE `openid` = '$openid'";
        return $conn->readConn($memberSql);
    }
    /**
     * 获取用户直推数量
     * entry uid
     * return array
     * */
    public static function getUserTeamCountInsideUid($conn,$uid){
        $querySql="SELECT count(`id`) as count FROM `users` WHERE  `pid` = $uid";
        return $conn->readConn($querySql)["count"];
    }
    /**
     * 获取用户直推单量
     * entry uid
     * return array
     * */
    public static function getUserActivationCountInsideUid($conn,$uid){
        $querySql="SELECT sum(`svip`) as `svip` FROM `users` WHERE  `pid` = $uid";
        return $conn->readConn($querySql)["svip"];
    }
    /**
     * 获取用户直推ids
     * entry uid
     * return array
     * */
    public static function getUserTeamCountIndirectInsideUid($conn,$uid){
        $querySql="SELECT GROUP_CONCAT(id) as ids FROM `users` WHERE  `pid` = $uid";
        return $conn->readConn($querySql)["ids"];
    }
    /**
     * 获取用户间推数量
     * entry uids
     * return array
     * */
    public static function getUserTeamCountInsideUids($conn,$uids){
        $querySql="SELECT count(`id`) as count FROM `users` WHERE  `pid` in($uids)";
        return $conn->readConn($querySql)["count"];
    }
    /**
     * 获取用户直推列表
     * entry UserExample->id
     * return array
     * */
    public static function getUserTeamInside($conn,$UserExample){
        $queryMemberSql="SELECT `id`,`username`,`truename`,`ach`,`sach`,`level`,`venturemoney`,`tinymoney`,`head`,`create_time`,`svip`,`service` FROM `users` WHERE  `pid` = $UserExample->id order by `id` desc";
        return $conn->readConn($queryMemberSql, true);
    }

    public static function getUserTeamCountInside($conn,$UserExample){
        $queryMemberSql="SELECT count(`id`) as count FROM `users` WHERE  `pid` = $UserExample->id";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户直推有效会员
     * entry UserExample->id
     * return array
     * */
    public static function getUserTeamLevelCountInside($conn,$UserExample){
        $queryMemberSql="SELECT count(`id`) as count FROM `users` WHERE  `pid` = '$UserExample->id' and `level` >0";
        return $conn->readConn($queryMemberSql)["count"];
    }
    /**
     * 获取用户直推激活人数
     * entry pid
     * return array
     * */
    public static function getUserPushActivationCountInside($conn,$pid){
        $queryMemberSql="SELECT count(`id`) as count FROM `users` WHERE  `pid` = $pid and `level` >0";
        return $conn->readConn($queryMemberSql)["count"];
    }
    /**
     * 获取用户直推激活代理数量
     * entry pid
     * return array
     * */
    public static function getUserAgentActivationCountInside($conn,$pid,$agent){
        $queryMemberSql="SELECT count(`id`) as count FROM `users` WHERE  `pid` = $pid and `agent` >$agent and `level` >0";
        return $conn->readConn($queryMemberSql)["count"];
    }
    /**
     * 获取用户旗下所有人数
     * entry UserExample->id
     * return array
     * */
    public static function getUserTeamCountLevelAllInside($conn,$UserExample){
        $queryMemberSql="SELECT	count(`id`) as count FROM (SELECT	@ids AS _ids,( SELECT @ids := GROUP_CONCAT( id ) FROM `users` WHERE FIND_IN_SET( pid, @ids ) ) AS cids,@l := @l + 1 AS layer FROM `users`,( SELECT @ids := $UserExample->id, @l := 0 ) b WHERE @ids IS NOT NULL) ID, `users` DATA WHERE FIND_IN_SET( DATA.id, ID._ids ) and id <> $UserExample->id";
        return $conn->readConn($queryMemberSql)["count"];
    }
    /**
     * 获取用户旗下所有人数
     * entry UserExample->id
     * return array
     * */
    public static function getUserTeamCountSvipAllInside($conn,$UserExample){
        $queryMemberSql="SELECT	sum(`svip`) as svip FROM (SELECT	@ids AS _ids,( SELECT @ids := GROUP_CONCAT( id ) FROM `users` WHERE FIND_IN_SET( pid, @ids ) ) AS cids,@l := @l + 1 AS layer FROM `users`,( SELECT @ids := $UserExample->id, @l := 0 ) b WHERE @ids IS NOT NULL) ID, `users` DATA WHERE FIND_IN_SET( DATA.id, ID._ids ) and id <> $UserExample->id";
        return $conn->readConn($queryMemberSql)["svip"];
    }
    /**
     * 获取用户旗下所有服务商人数
     * entry UserExample->id
     * return array
     * */
    public static function getUserTeamAgentCountAllInside($conn,$UserExample){
        $queryMemberSql="SELECT	count(`id`) as count FROM (SELECT	@ids AS _ids,( SELECT @ids := GROUP_CONCAT( id ) FROM `users` WHERE FIND_IN_SET( pid, @ids ) ) AS cids,@l := @l + 1 AS layer FROM `users`,( SELECT @ids := $UserExample->id, @l := 0 ) b WHERE @ids IS NOT NULL) ID, `users` DATA WHERE FIND_IN_SET( DATA.id, ID._ids ) and `service`>0";
        return $conn->readConn($queryMemberSql);
    }
    /**
     * 获取用户所有上级pid
     * entry id
     * return array
     * */
    public static function getUserSuperiorAllPid($conn,$id){
        $queryMemberSql="SELECT `id`,`token`,`level`,`svip`,`ach`,`one_ach`,`sach`,`service` FROM (SELECT @r AS _id,(SELECT @r := pid FROM users WHERE id = _id) AS parent_id,@l := @l + 1 AS lvl FROM (SELECT @r := $id, @l := 0) vars, users h WHERE @r <> 0) T1 JOIN users T2 ON T1._id = T2.id where 1=1 ORDER BY lvl ASC";
        return $conn->readConn($queryMemberSql,true);
    }
    /**
     * 获取用户所有上级sid
     * entry id
     * return array
     * */
    public static function getUserSuperiorAllSid($conn,$id){
        $queryMemberSql="SELECT `id`,`token`,`level`,`svip`,`ach`,`one_ach`,`sach`,`service`,`venturemoney`,`tinymoney`,`money` FROM (SELECT @r AS _id,(SELECT @r := sid FROM users WHERE id = _id) AS parent_id,@l := @l + 1 AS lvl FROM (SELECT @r := $id, @l := 0) vars, users h WHERE @r <> 0) T1 JOIN users T2 ON T1._id = T2.id where 1=1 ORDER BY lvl ASC";
        return $conn->readConn($queryMemberSql,true);
    }
    /**
     * 获取奖励教培费用户
     * entry id
     * return array
     * */
    public static function getUserEducation($conn,$nums){
        $queryMemberSql="SELECT `id`,`token`,`level`,`svip`,`ach`,`one_ach`,`sach`,`service`,`venturemoney`,`tinymoney`,`money`,`integral`,`partner_reward` FROM `users` WHERE `integral` > `venturemoney` order by `id` asc limit 0,$nums";
        return $conn->readConn($queryMemberSql,true);
    }
    /**
     * 获取公司充电站
     * entry conId
     * return array
     * */
    public static function getUserIdCardCountInside($conn,$id_card){
        $queryMemberSql="SELECT count(`id`) as count FROM `users` WHERE  `id_card` = '$id_card'";
        return $conn->readConn($queryMemberSql)["count"];
    }
    /**
     * 滑落人数增加
     * entry uid
     * return int
     * */
    public static function editUserSoneInside($conn,$uid){
        $upUserSql="UPDATE  `users` SET `sone` = `sone`+1 WHERE `id` = '$uid' and `sone` < 3";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 减去用户贡献值
     * entry UserExample
     * return int
     * */
    public static function editUserTinyMoneyInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `tinymoney` = `tinymoney`-$balance WHERE `token` = '$UserExample->token' and `tinymoney` = $UserExample->tinymoney";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户贡献值
     * entry UserExample
     * return int
     * */
    public static function editUserTinyMoneyAddInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `tinymoney` = `tinymoney`+$balance WHERE `token` = '$UserExample->token' and `tinymoney` = $UserExample->tinymoney";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户股权奖励比例
     * entry UserExample
     * return int
     * */
    public static function addUserShare($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `service` = `service` + $balance WHERE `token` = '$UserExample->token' and `integral` = $UserExample->integral";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户级别奖励
     * entry UserExample
     * return int
     * */
    public static function addUserLevel($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `integral` = `integral` + $balance,`agent_reward` = `agent_reward` + $balance,`total_reward`=`total_reward` + $balance,`venturemoney` = `venturemoney` - $balance WHERE `token` = '$UserExample->token' and `integral` = $UserExample->integral";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户分红奖励
     * entry UserExample
     * return int
     * */
    public static function addUserFenHong($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `integral` = `integral` + $balance,`push_reward` = `push_reward` + $balance,`total_reward`=`total_reward` + $balance ,`venturemoney` = `venturemoney` - $balance WHERE `token` = '$UserExample->token' and `integral` = $UserExample->integral";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户贡献奖励
     * entry UserExample
     * return int
     * */
    public static function addUserProffer($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `integral` = `integral` + $balance,`peer_reward` = `peer_reward` + $balance,`total_reward`=`total_reward` + $balance,`venturemoney` = `venturemoney` - $balance WHERE `token` = '$UserExample->token' and `integral` = $UserExample->integral";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户消费积分
     * entry UserExample
     * return int
     * */
    public static function editUserIntegralAddInside($conn,$integral,$UserExample){
        $upUserSql="UPDATE  `users` SET `integral` = `integral`+$integral WHERE `token` = '$UserExample->token' and `integral` = $UserExample->integral";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户业绩
     * entry UserExample
     * return int
     * */
    public static function editUserAchAddInside($conn,$ach,$oneMoney,$orderNumber,$UserExample){
        $upUserSql="UPDATE  `users` SET `ach` = `ach`+ $ach,`one_ach` = $oneMoney,`svip` = `svip` + $orderNumber WHERE `token` = '$UserExample->token' and `ach` = $UserExample->ach";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户业绩
     * entry UserExample
     * return int
     * */
    public static function editUserSachAddInside($conn,$ach,$UserExample){
        $upUserSql="UPDATE  `users` SET `sach` = `sach` + $ach WHERE `token` = '$UserExample->token' and `sach` = $UserExample->sach";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 减去用户余额
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`-$balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 转出
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyOutInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`-$balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 增加用户余额
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyAddInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 增加用户余额和已发教培奖
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyAndVenturemoneyInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$balance,`tinymoney` = `tinymoney` - $balance,`partner_reward` = `partner_reward` + $balance,`total_reward` = `total_reward` + $balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 增加用户冻结教培奖
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyAndVentureInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `tinymoney` = `tinymoney`+$balance,`venturemoney` = `venturemoney` + $balance WHERE `token` = '$UserExample->token' and `tinymoney` = $UserExample->tinymoney";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 增加用户余额和已发教培奖(解冻)
     * entry UserExample
     * return int
     * */
    public static function editUserMoneyThawInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$balance,`tinymoney` = `tinymoney` - $balance,`venturemoney` = `venturemoney`+$balance,`partner_reward` = `partner_reward` + $balance,`total_reward` = `total_reward` + $balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `tinymoney` = '$UserExample->tinymoney' ";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 兑换
     * entry UserExample
     * return int
     * */
    public static function editUserExchangeInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money` + $balance,`integral` = `integral` - $balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `integral` = $UserExample->integral";
        return $conn->executeConn($upUserSql);
    }
    /**
     * 升级
     * entry UserExample
     * return int
     * */
    public static function editUserLevelInside($conn,$educationLimit,$UserExample){
        $joinSql = ",`level_time` = NOW() ";
        $upUserSql="UPDATE  `users` SET `level` = `level` + 1,`integral` = $educationLimit $joinSql WHERE `token` = '$UserExample->token' and `level` = '$UserExample->level'";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 结算直推上级奖励
     * entry UserExample
     * return int
     * */
    public static function editUserPidSettlementInside($conn,$reward,$service,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money` + $reward,`share_rewards` = `share_rewards` + $reward ,`total_reward` = `total_reward` + $reward,`service` = `service` + $service WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 结算定额提成
     * entry UserExample
     * return int
     * */
    public static function editUserSettlementInside($conn,$reward,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$reward,`push_reward` = `push_reward` + $reward,`total_reward`=`total_reward` + $reward WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `push_reward` = $UserExample->push_reward";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 结算管理费
     * entry UserExample
     * return int
     * */
    public static function editUserAgentSettlementInside($conn,$reward,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money` + $reward,`agent_reward` = `agent_reward` + $reward,`total_reward`=`total_reward` + $reward  WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `agent_reward` = $UserExample->agent_reward";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 结算广告费
     * entry UserExample
     * return int
     * */
    public static function editUserLevelAgentSettlementInside($conn,$reward,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money` + $reward,`peer_reward` = `peer_reward` + $reward,`total_reward`=`total_reward` + $reward  WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `peer_reward` = $UserExample->peer_reward";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 直推结算
     * entry UserExample
     * return int
     * */
    public static function editUserZTSettlementInside($conn,$reward,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$reward,`integral` = `integral` - $reward,`total_reward`=`total_reward` + $reward WHERE `token` = '$UserExample->token' and `money` = $UserExample->money and `integral` = $UserExample->integral";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 增加用户直推奖励
     * entry UserExample
     * return int
     * */
    public static function editUserShareRewardsInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$balance,`share_rewards` = `share_rewards`+$balance,`total_reward`=`total_reward`+$balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 增加用户间推奖励
     * entry UserExample
     * return int
     * */
    public static function editUserPushRewardInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$balance,`push_reward` = `push_reward`+$balance,`total_reward`=`total_reward`+$balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 增加用户服务商奖励
     * entry UserExample
     * return int
     * */
    public static function editUserServiceRewardInside($conn,$balance,$UserExample){
        $upUserSql="UPDATE  `users` SET `money` = `money`+$balance,`agent_reward` = `agent_reward`+$balance,`total_reward`=`total_reward`+$balance WHERE `token` = '$UserExample->token' and `money` = $UserExample->money";
        return $conn->executeConn($upUserSql);;
    }
    /**
     * 获取级别总贡献值
     * entry level
     * return array
     * */
    public static function getUserSumTinyMoneyLevel($conn,$level){
        $querySql="SELECT sum(`tinymoney`) as tinymoney FROM `users` WHERE  `level` = $level";
        return $conn->readConn($querySql)["tinymoney"];
    }
    /**
     * 获取所有级别总贡献值
     * entry level
     * return array
     * */
    public static function getUserSumTinyMoneyAll($conn){
        $querySql="SELECT sum(`tinymoney`) as tinymoney FROM `users` WHERE  `level` > 0";
        return $conn->readConn($querySql)["tinymoney"];
    }
    /**
     * 获取用户今日新增贡献奖励(有效)
     * entry uid
     * return array
     * */
    public static function getUserSumTinyMoneyToday($conn){
        $today = strtotime(date("Y-m-d"),time());
        $querySql="SELECT sum(`tinymoney`) as tinymoney FROM `users` WHERE  `id` in (SELECT distinct `pid` FROM `users` WHERE  UNIX_TIMESTAMP(`level_time`) >$today) and `level` >0";
        return $conn->readConn($querySql)["tinymoney"];
    }
    /**
     * 获取级别总贡献值(最高)
     * entry level
     * return array
     * */
    public static function getUserSumTinyMoneyLevelMost($conn){
        $querySql="SELECT sum(`tinymoney`) as tinymoney ,level FROM `users` GROUP BY level ORDER BY `level` desc limit 1";
        return $conn->readConn($querySql);
    }
    /**
     * 获取级别所有会员
     * entry level
     * return array
     * */
    public static function getUserLevel($conn,$level){
        $querySql="SELECT `id`,`token`,`username`,`level`,`venturemoney`,`tinymoney`,`integral`,`service` FROM `users` WHERE  `level` = $level and `tinymoney` > 0";
        return $conn->readConn($querySql,true);
    }
    /**
     * 获取级别所有会员
     * entry level
     * return array
     * */
    public static function getUserLevelAll($conn){
        $querySql="SELECT `id`,`token`,`username`,`level`,`venturemoney`,`tinymoney`,`integral` FROM `users` WHERE  `level` > 0 and `tinymoney` > 0";
        return $conn->readConn($querySql,true);
    }
    /**
     * 获取今日推荐用户列表
     * entry level
     * return array
     * */
    public static function getUserLevelAllToday($conn){
        $today = strtotime(date("Y-m-d"),time());
        $querySql="SELECT `id`,`token`,`username`,`level`,`venturemoney`,`tinymoney`,`integral` FROM `users` WHERE  `id` in (SELECT distinct `pid` FROM `users` WHERE  UNIX_TIMESTAMP(`level_time`) >$today) and `level` >0";
        return $conn->readConn($querySql,true);
    }
    /**
     * 获取未回本所有会员
     * entry level
     * return array
     * */
    public static function getUserNoRecovery($conn){
        $querySql="SELECT `id`,`token`,`username`,`level`,`venturemoney`,`tinymoney`,`integral`,`ach`,`total_reward` FROM `users` WHERE `ach` >0 AND `ach` > `total_reward`";
        return $conn->readConn($querySql,true);
    }
    /**
     * 修改用户信息可添加字段
     * entry UserExample
     * return int
     * */
    public static function upUserInside($conn,$UserExample){
        $joinSql="";
        if($UserExample->truename !=null){
            $joinSql.=" `truename` = '$UserExample->truename',";
        }
        if($UserExample->id_card !=null){
            $joinSql.=" `id_card` = '$UserExample->id_card',";
        }
        if($UserExample->password !=null){
            $UserExample->password = md5(md5($UserExample->password));
            $joinSql.=" `password` = '$UserExample->password',";
        }
        if($UserExample->pay_password !=null){
            $UserExample->pay_password = md5(md5($UserExample->pay_password));
            $joinSql.=" `pay_password` = '$UserExample->pay_password',";
        }
        if($UserExample->login_ip !=null){
            $joinSql.=" `login_ip` = '$UserExample->login_ip',";
        }
        if($UserExample->last_time !=null){
            $joinSql.=" `last_time` = $UserExample->last_time,";
        }
        if($UserExample->pid !=null){
            $joinSql.=" `pid` = $UserExample->pid,";
        }
        if($UserExample->head !=null){
            $joinSql.=" `head` = '$UserExample->head',";
        }
        if($UserExample->level_time !=null){
            $joinSql.=" `level_time` = $UserExample->level_time,";
        }
        if($UserExample->level !=null){
            $joinSql.=" `level` = $UserExample->level,";
        }
        if($UserExample->sid !=null){
            $joinSql.=" `sid` = $UserExample->sid,";
        }
        if($UserExample->sone !=null){
            $joinSql.=" `sone` = $UserExample->sone,";
        }
        if($UserExample->service !=null){
            $joinSql.=" `service` = $UserExample->service,";
        }
        if($UserExample->status !=null){
            $joinSql.=" `status` = $UserExample->status,";
        }
        if($UserExample->sach !=null){
            $joinSql.=" `sach` = $UserExample->sach,";
        }
        if($joinSql !=null){
            $innerJoin = rtrim($joinSql,",");
            $upUserSql="UPDATE  `users` SET $innerJoin WHERE `token` = '$UserExample->token'";
            return $conn->executeConn($upUserSql);
        }
    }
    /**
     * 获取用户实名认证次数
     * entry conn,uid
     * return array
     * */
    public static function getCertificationInside($conn,$uid){
        $querySql="SELECT `num` FROM `certification` WHERE  `uid` = $uid";
        return $conn->readConn($querySql)["num"];
    }
    /**
     * 插入用户实名认证次数
     * entry conn,uid
     * return int
     * */
    public static function insertCertificationInside($conn,$uid){
        $insertSql = "insert into `certification`(`uid`)value($uid)";
        return $conn->executeConn($insertSql);
    }
    /**
     * 更新用户实名认证次数
     * entry conn,uid
     * return int
     * */
    public static function editCertificationInside($conn,$uid){
        $upSql = "UPDATE  `certification` SET `num` = `num`+1 WHERE `uid` = $uid";
        return $conn->executeConn($upSql);
    }
}